<?php


namespace app\controller\admin;


use app\models\Baseinfo;
use app\services\BaseinfoService;
use app\validates\BaseinfoValidate;

class BaseinfoController {
    public function index() {
        return view('/admin/baseinfo/lists');
    }

    public function lists() {
        // 接收参数
        $student = trim(request()->param('student', ''));
        $page = intval(request()->param('page', 1));
        $limit = intval(request()->param('limit', 20));
        // 逻辑处理
        $res = BaseinfoService::getPageList($student, $page, $limit);
        // 返回数据
        return successJson($res);
    }

    public function add() {
        return view('/admin/baseinfo/add');
    }

    public function create() {
        // 接收参数
        $student = trim(request()->param('student', ''));
        $password = trim(request()->param('password', ''));
        $uuid = trim(request()->param('uuid', ''));
        $serial = trim(request()->param('serial', ''));
        $classes = trim(request()->param('classes', ''));
        $phone = trim(request()->param('phone', ''));
        $address = trim(request()->param('address', ''));
        $guardian = trim(request()->param('guardian', ''));
        $status = intval(request()->param('status', 0));
        $note = trim(request()->param('note', ''));
        // 参数验证
        validate(BaseinfoValidate::class)->scene('add')->check([
            'student' => $student,
            'password' => $password,
        ]);
        // 逻辑处理
        $res = BaseinfoService::createData($student, $password, $uuid, $serial, $classes, $phone, $address, $guardian, $status, $note);
        // 返回数据
        return successJson($res);
    }

    public function edit() {
        // 接收参数
        $id = intval(request()->param('id', 0));
        $info = Baseinfo::getInfoById($id);
        return view('/admin/baseinfo/edit', ['info' => $info]);
    }

    public function update() {
        // 接收参数
        $id = intval(request()->param('id', 0));
        $student = trim(request()->param('student', ''));
        $password = trim(request()->param('password', ''));
        $uuid = trim(request()->param('uuid', ''));
        $serial = trim(request()->param('serial', ''));
        $classes = trim(request()->param('classes', ''));
        $phone = trim(request()->param('phone', ''));
        $address = trim(request()->param('address', ''));
        $guardian = trim(request()->param('guardian', ''));
        $status = intval(request()->param('status', 0));
        $note = trim(request()->param('note', ''));
        // 参数验证
        validate(BaseinfoValidate::class)->scene('edit')->check([
            'student' => $student,
            'id' => $id,
        ]);
        // 逻辑处理
        $res = BaseinfoService::updateData($id, $student, $password, $uuid, $serial, $classes, $phone, $address, $guardian, $status, $note);
        // 返回数据
        return successJson($res);
    }

    public function delete() {
        // 接收参数
        $id = intval(request()->param('id', 0));
        // 参数验证
        validate(BaseinfoValidate::class)->scene('delete')->check([
            'id' => $id,
        ]);
        // 逻辑处理
        $res = BaseinfoService::deleteData($id);
        // 返回数据
        return successJson($res);
    }
}